//
//  MYLine.m
// 
//
//  Created by Robin on 16/12/10.
//  Copyright © 2016年 HolyShit. All rights reserved.
//

#import "MYLine.h"

MYPading MYPadingMake(CGFloat _left, CGFloat _right, CGFloat _top, CGFloat _bottom)
{
    MYPading padding;
    padding.left = _left;
    padding.right = _right;
    padding.top = _top;
    padding.bottom = _bottom;
    return padding;
}

@interface MYLine ()

@property (nonatomic, assign) BOOL bgColorLock;

@end

@implementation MYLine

-(instancetype)init
{
    if(self = [super init]){
        [self startInit];
    }
    return self;
}

-(void)startInit
{
    [self manualSetBgColor:HEXCOLOR(HSColorSeparateDark)];
    CGFloat onePx = 1 / MIN([UIScreen mainScreen].scale, 2.0);
    self.size = CGSizeMake(onePx, onePx);
}

//背景色自己控制，不然cell选中的时候会被控制
-(void)manualSetBgColor:(UIColor *)color
{
    self.bgColorLock = NO;
    self.backgroundColor = color;
    self.bgColorLock = YES;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
    //未锁定才允许修改背景色
    if(!self.bgColorLock){
        [super setBackgroundColor:backgroundColor];
    }
}

@end

const NSUInteger MYLineTopTag = 23333 + 'T';
const NSUInteger MYLineBottomTag = 23333 + 'B';
const NSUInteger MYLineLeftTag = 23333 + 'L';
const NSUInteger MYLineRightTag = 23333 + 'R';

@implementation UIView (MY_ShowLine)

-(void)showLineForHorizontalWithLeftPad:(CGFloat)pad
{
    [self showLineForDirection:MYDirectionTop pading:MYPadingMake(pad, 0, 0, 0)];
    [self showLineForDirection:MYDirectionBottom pading:MYPadingMake(pad, 0, 0, 0)];
}

-(void)showLineForDirection:(MYDirection)direction leftPad:(CGFloat)pad
{
    [self showLineForDirection:direction pading:MYPadingMake(pad, 0, 0, 0)];
}

-(void)showLineForDirection:(MYDirection)direction
{
    [self showLineForDirection:direction pading:MYPadingMake(0, 0, 0, 0)];
}

-(void)showLineForDirection:(MYDirection)direction pading:(MYPading)pading
{
//    CGFloat onePx = 1 / MIN([UIScreen mainScreen].scale, 2.0);
    CGFloat onePx = 1 / [UIScreen mainScreen].scale;
    void(^MASBlock)(MASConstraintMaker *make) = ^(MASConstraintMaker *make){
        switch (direction) {
            case MYDirectionTop:{
                make.left.mas_equalTo(pading.left);
                make.right.mas_equalTo(-pading.right);
                make.top.mas_equalTo(0);
                make.height.mas_equalTo(onePx);
            }
                break;
            case MYDirectionBottom:{
                make.left.mas_equalTo(pading.left);
                make.right.mas_equalTo(-pading.right);
                make.bottom.mas_equalTo(0);
                make.height.mas_equalTo(onePx);
            }
                break;
            case MYDirectionLeft:{
                make.top.mas_equalTo(pading.top);
                make.bottom.mas_equalTo(-pading.bottom);
                make.left.mas_equalTo(0);
                make.width.mas_equalTo(onePx);
            }
                break;
            case MYDirectionRight:{
                make.top.mas_equalTo(pading.top);
                make.bottom.mas_equalTo(-pading.bottom);
                make.right.mas_equalTo(0);
                make.width.mas_equalTo(onePx);
            }
                break;
        }
    };
    MYLine *line = [self lineWithDirection:direction];
    if(line){
        line.hidden = NO;
        [line mas_updateConstraints:MASBlock];
    }
    else{
        //初始化
        line = [[MYLine alloc]init];
        line.tag = [self lineTagWithDirection:direction];
        [self addSubview:line];
        [line mas_makeConstraints:MASBlock];
    }
    [self bringSubviewToFront:line];
}

-(void)showLineForVertical
{
    [self showLineForDirection:MYDirectionLeft];
    [self showLineForDirection:MYDirectionRight];
}

-(void)showLineForHorizontal
{
    [self showLineForDirection:MYDirectionTop];
    [self showLineForDirection:MYDirectionBottom];
}

-(void)hideLineForDirection:(MYDirection)direction
{
    MYLine *line = [self lineWithDirection:direction];
    if(line){
        line.hidden = YES;
    }
}

-(void)hideLineForVertical
{
    [self hideLineForDirection:MYDirectionLeft];
    [self hideLineForDirection:MYDirectionRight];
}

-(void)hideLineForHorizontal
{
    [self hideLineForDirection:MYDirectionTop];
    [self hideLineForDirection:MYDirectionBottom];
}

-(MYLine *)lineWithDirection:(MYDirection)direction
{
    return (MYLine *)[self viewWithTag:[self lineTagWithDirection:direction]];
}

-(NSUInteger)lineTagWithDirection:(MYDirection)direction
{
    switch (direction) {
        case MYDirectionTop:
            return MYLineTopTag;
        case MYDirectionBottom:
            return MYLineBottomTag;
        case MYDirectionLeft:
            return MYLineLeftTag;
        case MYDirectionRight:
            return MYLineRightTag;
    }
    return 0;
}

@end
